package com.tuanbiao.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.hzjq.data.entity.UIDRuleEntity;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.EncodingRulesController;
import com.tuanbiao.data.entity.CodeModelType;
import com.tuanbiao.databinding.ActivityEncodingRulesBinding;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.dialog.SelectDialog;
import com.tuanbiao.models.EncodingRulesModel;
import com.tuanbiao.presenter.EncodingRulesPresenter;
import com.tuanbiao.ui.account.LoginActivity;
import com.tuanbiao.utils.LibCollections;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class EncodingRulesActivity extends BasePresenterActivity<EncodingRulesPresenter, ActivityEncodingRulesBinding> implements EncodingRulesController.View {
    private CodeModelType codeModelType;
    private boolean mIsFromSetting;

    public static void start(Context context) {
        Intent starter = new Intent(context, EncodingRulesActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected EncodingRulesPresenter createPresenter() {
        return new EncodingRulesPresenter(new EncodingRulesModel(), this);
    }

    @Override
    protected ActivityEncodingRulesBinding getBindingView() {
        return ActivityEncodingRulesBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.tvEncodingRules.setOnClickListener(view -> {
            mPresenter.showEnCodingRulesDialog();

        });
        binding.btnSave.setOnClickListener(view -> {
            new MessageDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setMessage("确定保存")
                    .setListener(dialog -> {
                        mPresenter.saveMillCode(codeModelType, getString(binding.etModuleMillCode), getString(binding.etDetonatorMill)
                                , getString(binding.etSignature), getString(binding.etModuleType));
                    })
                    .show();

        });
    }

    @Override
    public void showDetails(UIDRuleEntity entity) {
        codeModelType = new CodeModelType(entity);
        binding.tvEncodingRules.setText(entity.getRuleName());
        binding.etModuleMillCode.setText(entity.getModuleMillCode());
        binding.etDetonatorMill.setText(entity.getCapMillCode());
        binding.etSignature.setText(entity.getSignatureCode());
        binding.etModuleType.setText(entity.getModuleTypeCode());
    }

    @Override
    public void showEncodingRulesDialog(List<CodeModelType> codeModels) {
        int select = 0;
        for (int i = 0; i < LibCollections.size(codeModels); i++) {
            if (null == codeModelType) {
                break;
            }
            if (TextUtils.equals(codeModelType.itemName(), codeModels.get(i).itemName())) {
                select = i;
                break;
            }
        }

        // 单选对话框
        new SelectDialog.Builder(this)
                .setTitle("请选择编码规则")
                .setList(codeModels)
                // 设置单选模式
                .setSingleSelect()
                .setSelect(select)
                // 设置默认选中
//                    .setSelect(select)
                .setListener((SelectDialog.OnListener<CodeModelType>) (dialog, data) -> {
                    Set<Map.Entry<Integer, CodeModelType>> entries = data.entrySet();
                    for (Map.Entry<Integer, CodeModelType> entry : entries) {
                        codeModelType = entry.getValue();
                    }
                    showDetails(codeModelType.getName());
                })
                .show();
    }

    @Override
    public void showFromSetting(boolean isFromSetting) {
        mIsFromSetting = isFromSetting;
        binding.ivToolbarBack.setVisibility(isFromSetting ? View.VISIBLE : View.GONE);
    }

    @Override
    public void startMain() {
        MainActivity.start(getActivity());
    }

    @Override
    public void startLogin() {
        LoginActivity.start(getActivity());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!mIsFromSetting && keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}